import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime
from Mr_Zhong.utils.log import Logger
from sklearn.preprocessing import StandardScaler
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.metrics import roc_auc_score
from sklearn.utils.class_weight import compute_sample_weight
import joblib



plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['font.size'] = 15

class PowerLoadModel(object):
    def __init__(self, filename):
        # 配置日志记录
        logfile_name = "train_" + datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        self.logfile = Logger('../', logfile_name).get_logger()
        # 获取数据源
        self.data_source = pd.read_csv(filename, encoding='utf-8')

def feature_engineering(data, logger):
    logger.info('===============开始进行特征工程处理===============')
    result = data.copy(deep=True)
    logger.info("===============开始处理数据特征===================")
    # 1.提取出特征
    X_train = result.loc[:, ['Age', 'BusinessTravel', 'Department', 'DistanceFromHome', 'Education',
                             'EducationField', 'EnvironmentSatisfaction', 'Gender', 'JobInvolvement', 'JobLevel',
                             'JobRole', 'JobSatisfaction', 'MaritalStatus', 'MonthlyIncome', 'NumCompaniesWorked',
                             'OverTime', 'RelationshipSatisfaction', 'StockOptionLevel', 'TotalWorkingYears', 'TrainingTimesLastYear',
                             'WorkLifeBalance', 'YearsAtCompany', 'YearsInCurrentRole', 'YearsSinceLastPromotion', 'YearsWithCurrManager']]
    Y_train = result.iloc[:, 0]

    # 1.1修改BusinessTravel中的值, Non-Travel,Travel_Rarely,Travel_Frequently
    travel_BusinessTravel_map = {
        'Non-Travel': 0,
        'Travel_Rarely': 1,
        'Travel_Frequently': 2
    }
    X_train['BusinessTravel'] = X_train['BusinessTravel'].map(travel_BusinessTravel_map)

    # 1.2修改Department中的值, Human Resources, Research & Development , Sales
    travel_Department_map = {
        'Human Resources': 1,
        'Research & Development': 2,
        'Sales': 3
    }
    X_train['Department'] = X_train['Department'].map(travel_Department_map)

    # 1.3修改EducationField中的值, Life Sciences , Medical , Marketing, Technical Degree, Other, Human Resources
    travel_EducationField_map = {
        'Life Sciences': 1,
        'Medical': 2,
        'Marketing': 3,
        'Technical Degree': 4,
        'Other': 5,
        'Human Resources': 6
    }
    X_train['EducationField'] = X_train['EducationField'].map(travel_EducationField_map)

    # 1.4修改Gender中的值, Male,female
    travel_Gender_map = {
        'Male': 1,
        'Female': 0,
    }
    X_train['Gender'] = X_train['Gender'].map(travel_Gender_map)

    # 1.5修改JobRole中的值, Sales Executive ,Research Scientist,Laboratory Technician,Manufacturing Director ,
    # Healthcare Representative ,Manager ,Sales Representative  ,Research Director ,Human Resources
    travel_JobRole_map = {
        'Sales Executive': 1,
        'Research Scientist': 2,
        'Laboratory Technician': 3,
        'Manufacturing Director': 4,
        'Healthcare Representative': 5,
        'Manager': 6,
        'Sales Representative': 7,
        'Research Director': 8,
        'Human Resources': 9
    }
    X_train['JobRole'] = X_train['JobRole'].map(travel_JobRole_map)

    # 1.6修改MaritalStatus中的值, Married ,Single ,Divorced
    travel_MaritalStatus_map = {
        'Divorced': 0,
        'Single': 1,
        'Married': 2
    }
    X_train['MaritalStatus'] = X_train['MaritalStatus'].map(travel_MaritalStatus_map)

    # 1.7修改OverTime中的值, No, Yes
    travel_OverTime_map = {
        'No': 0,
        'Yes': 1
    }
    X_train['OverTime'] = X_train['OverTime'].map(travel_OverTime_map)

    X_train.to_csv('X_train.csv', index=False)


    return X_train, Y_train
